{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "71fd0110-d53b-44b5-b8e5-e86b0dcb1365",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# https://mp.weixin.qq.com/s?__biz=MjM5MDEzNDAyNQ==&mid=402378855&idx=1&sn=77ed3c403aa00977e66a6d712b565f44&scene=21#wechat_redirect\n",
    "# 统计师的Python日记【第3天：Numpy你好】\n",
    "import numpy as np\n",
    "\n",
    "data = [1, 2, 3]\n",
    "a = np.array(data)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9176953b-4a59-4428-a0cd-7ab875131ac2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = [[1, 2, 3], [34, 5, 6]]\n",
    "b = np.array(data2)\n",
    "b\n",
    "b.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1d0b37b2-43bd-41f9-bc9e-8e8502225d5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('<U4')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([\"shu\", \"shuo\", \"jun\"])\n",
    "c.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "59e1136a-7c36-4e3d-a3ba-608582bb0e10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组运算\n",
    "# 加、减、乘、除、内积、转置\n",
    "a = [[1, 2, 3], [4, 5, 6]]\n",
    "a + a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "03383b26-4c62-4649-8ae8-31e124369292",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  6],\n",
       "       [ 8, 10, 12]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "npa = np.array(a)\n",
    "npa + npa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a8c53c05-3764-42f5-9c1c-6b280c03884b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "npa - npa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "89221d23-74f3-4cb0-8c44-5221261b7b4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4,  9],\n",
       "       [16, 25, 36]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "npa * npa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "127f24cb-a8d6-403e-a0f2-55a6cb874b15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[14 32]\n",
      " [32 77]]\n",
      "x阶数：(2, 3)\n",
      "y阶数：(3, 2)\n",
      "result阶数：(2, 2)\n"
     ]
    }
   ],
   "source": [
    "# https://zhuanlan.zhihu.com/p/353753915\n",
    "print(npa)\n",
    "result = np.dot(npa, npa.T)\n",
    "print(result)\n",
    "print(\"x阶数：\" + str(npa.shape))\n",
    "print(\"y阶数：\" + str(npa.T.shape))\n",
    "print(\"result阶数：\" + str(result.shape))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "63fc2658-2b5c-455d-9c08-64e08f0967ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8],\n",
       "       [ 9, 10, 11, 12]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 拆分\n",
    "x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])\n",
    "x.reshape((3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "63fd5b98-d87d-4c22-884c-1d58e329ef23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 1.41421356, 1.73205081],\n",
       "       [2.        , 2.23606798, 2.44948974],\n",
       "       [2.64575131, 2.82842712, 3.        ]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 开方\n",
    "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "np.sqrt(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4cc4edd4-8f4a-4ccc-bfa2-2b2959cabe21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  2,  3],\n",
       "       [ 9,  8,  6],\n",
       "       [10, 15,  7]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sort()排序\n",
    "# sort(a,0)是对竖轴上的元素进行排序；sort(a,1)是对横轴上的元素进行排序.\n",
    "a = np.array([[10, 2, 3], [4, 15, 6], [9, 8, 7]])\n",
    "np.sort(a, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fcefbdb7-2120-4ab2-a693-676ee831b3ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  3, 10],\n",
       "       [ 4,  6, 15],\n",
       "       [ 7,  8,  9]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(a, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "20042230-6c5b-4905-9063-3b66cc735b19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(7.111111111111111)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 均值\n",
    "np.mean(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a292f2af-ef36-4ca2-98b1-709525ffcb6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(64)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sum\n",
    "np.sum(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "76ebbb1a-0aed-4540-9c8a-9f1f19f62773",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(3.7843080813169783)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准差\n",
    "# 计算平均值（mean）。\n",
    "# 计算每个数据点与平均值之差的平方。\n",
    "# 计算这些平方差的平均值（方差）。\n",
    "# 对方差进行平方根运算，得到标准差。\n",
    "np.std(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7470fec6-1b91-4847-8ad3-5d13f932e748",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10  2  3]\n",
      " [ 4 15  6]\n",
      " [ 9  8  7]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([5.        , 8.33333333, 8.        ])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对行或者列进行统计计算，同样指定0和1即可\n",
    "print(a)\n",
    "np.mean(a, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4b2a9f0f-2586-4206-8885-d2ab7b5f54a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.31429832, -0.04020686,  1.82528265,  3.35060702],\n",
       "       [ 2.60161969,  1.42933808,  1.21295463,  3.21813097],\n",
       "       [ 2.76619126,  3.13526801,  1.76623261,  0.72635251]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成一个m×n矩阵，服从均值mean，标准差std的正态分布\n",
    "aa = np.random.normal(2, 1, size=(3, 4))\n",
    "aa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c997a8f9-7415-4850-965b-e21efd977346",
   "metadata": {},
   "outputs": [],
   "source": [
    "# save\n",
    "np.save(\"save_aa\", aa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5034e59-6ba5-47df-b80d-642b71cc5edc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# npmpy 牛，数学知识补充中"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
